﻿#r @"..\Microsoft.SharePoint.Client (spo)\Microsoft.SharePoint.Client.dll"
#r @"..\Microsoft.SharePoint.Client (spo)\Microsoft.SharePoint.Client.Runtime.dll"

#load "Client.fs"

open Client
open Microsoft.SharePoint.Client
open Microsoft.SharePoint.Client.Application
open System.Security;

// =======================================================
// Modifying SharePoint columns
// =======================================================

let web = Client.Context.Web
Client.Load web

let list = Client.Context.Web.Lists.GetByTitle("Documents")

Client.Load list
Client.Load list.Fields

// Read all fields
for spField in list.Fields do 
    Client.Load spField
    printfn "%s\n" spField.Title

printfn "%s\n" "Title|FieldTypeKind|Sortable|Filterable"
for spField in list.Fields do 
    Client.Load spField
    let msg = spField.Title + "|" + spField.FieldTypeKind.ToString() + "|" + spField.Sortable.ToString() + "|" + spField.Filterable.ToString()
    printfn "%s\n" msg

// Load existing field
let fld = list.Fields.GetByInternalNameOrTitle("_dlc_DocId")
Client.Load fld
fld.Hidden

// Add field
let userFieldXml = @"<Field Name=""ABC"" DisplayName=""ABC""
    Description=""Bla bla bla""
    Type=""User"" Mult=""True"" Required=""FALSE"" ShowField=""Name""
    Group=""Configuration List Columns"">
    </Field>"
let userField = list.Fields.AddFieldAsXml(userFieldXml, false, AddFieldOptions.DefaultValue)
Client.ExecuteQuery()

Client.Load userField
Client.ExecuteQuery()

userField.SchemaXml
userField.InternalName
userField.Description <- "Bla bla bla"
userField.SetShowInNewForm(true)
userField.SetShowInEditForm(true)
userField.SetShowInDisplayForm(true)
userField.Hidden <- true
userField.ReadOnlyField <- true
userField.Indexed <- true
userField.DefaultValue <- null
userField.Required <- false
userField.Update()
Client.ExecuteQuery()

fld.Update()
Client.ExecuteQuery()

let deletedField = list.Fields.GetByInternalNameOrTitle("DeletedField")
deletedField.DeleteObject()
Client.ExecuteQuery()

let assetList = web.Lists.GetByTitle("Site Assets")
let RoleDefinitionsFldLookup = Client.Context.CastTo<FieldLookup>(userField)
Client.Load RoleDefinitionsFldLookup
RoleDefinitionsFldLookup.LookupList <- assetList.Id.ToString()
RoleDefinitionsFldLookup.LookupField <- "Title"
RoleDefinitionsFldLookup.Update()
Client.ExecuteQuery()


let BooleanField = @"<Field StaticName=""BooleanField"" DisplayName=""BooleanField"" Description="""" Type=""Boolean""  Required=""FALSE"">
        <Default>0</Default></Field>";
list.Fields.AddFieldAsXml(BooleanField, true, AddFieldOptions.DefaultValue)
Client.ExecuteQuery()

let linkField = @"<Field Type=""URL"" DisplayName=""LinkField"" Required=""FALSE"" ReadOnly=""TRUE"" ShowInEditForm=""FALSE""
        Description=""Gives current status of site creation process like site creationf pending, site creation in process, completed, etc.""
        Indexed=""FALSE"" Format=""Hyperlink"" StaticName=""LogFile"" Name=""LogFile"" >
        </Field>"
let LogFilefld = list.Fields.AddFieldAsXml(linkField, true, AddFieldOptions.DefaultValue)
Client.Load LogFilefld

